package slxy.jiangtao.dts.controller.business;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.controller.BaseController;
import slxy.jiangtao.dts.core.common.bean.Constants;
import slxy.jiangtao.dts.core.common.bean.FormCriteria;
import slxy.jiangtao.dts.core.common.bean.FormQuery;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.hibernate.qbc.Page;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.DateUtils;
import slxy.jiangtao.dts.entity.business.Meet;
import slxy.jiangtao.dts.service.sys.MeetService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/5/16.
 * 说明：
 */
@Controller
@RequestMapping("meet")
public class MeetController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(MeetController.class);
    @Autowired
    CommonService commonService;
    @Autowired
    MeetService meetService;

    /**
     * 会议列表页面
     */
    @RequestMapping("index")
    public String index(ModelMap modelMap) {

        FormQuery formQuery = new FormQuery("business.Meet");
        ArrayList<FormCriteria> formCriterias = new ArrayList<>();
        //默认只显示本月召开的会议
        formCriterias.add(new FormCriteria("meetTime", "like", DateUtils.getDate("yyyy-MM") + "%"));
        Page page = commonService.getPage(formQuery);
        modelMap.addAttribute("page", JSON.toJSONString(page));
        return "meet/index";
    }

    /**
     * 会议添加跳转页面
     */
    @RequestMapping("addPage")
    public String addPage() {
        return "meet/add";
    }

    /**
     * 会议添加跳转页面
     */
    @RequestMapping("page/{forword}")
    public String Page(ModelMap modelMap, @PathVariable String forword, HttpServletRequest request) {
        if (forword.trim().equals("update")) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            modelMap.put("meet", commonService.get(Meet.class, id));
        }
        return "meet/" + forword.trim().toLowerCase();
    }

    @RequestMapping("submit/{method}")
    @ResponseBody
    public JSONResult submit(@PathVariable String method, Meet meet, HttpServletRequest request) {
        Integer id = meet.getId();
        try {
            if (method.trim().equals("add") || method.trim().equals("update")) {
                id = meetService.addOrUpdate(meet, getUserInSession(request));
            } else{
                return JSONResult.failure("无效访问路径");
            }
        } catch (Exception e) {
            logger.error("会议操作失败", e);
            return JSONResult.failure("操作失败");
        }
        return JSONResult.success(id);
    }

    @RequestMapping("delete/{meetid}")
    @ResponseBody
    public JSONResult delete(@PathVariable Integer meetid, HttpServletRequest request) {
        try {
            meetService.delete(meetid);
            logger.info(getUserInSession(request).getName() + "删除一个会议");
        } catch (Exception e) {
            logger.error("会议删除失败", e);
            return JSONResult.failure("会议删除失败");
        }
        return JSONResult.success();
    }

    @RequestMapping("detail/{meetid}")
    public String detail(@PathVariable Integer meetid, ModelMap modelMap) {
        Meet meet = commonService.get(Meet.class, meetid);
        commonService.evict(meet);
        String context = Constants.WORD_TITLE_LEFT+meet.getName()+Constants.WORD_TITLE_RIGHT
                +meet.getContent();
        meet.setContent(context);
        modelMap.put("meet",meet );
        return "meet/detail";
    }


}
